﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using System.Transactions;
using WebApp.Commons;
using WebApp.Data;

namespace WebApp.Services
{
    public class BlockService
    {
        #region | Index |

        public List<tbl_block> BlockList(int index,
                                                   int size,
                                                   string sortColumn,
                                                   string sortDirection,
                                                   string keyword,
                                                   ref int count)
        {
            using (var db = new PAVINGDataContext())
            {
                var direction = sortDirection.Equals("desc") ? " descending " : "";

                var mat = db.tbl_blocks.Where(u => u.block_code.Contains(keyword))
                                              .OrderBy(sortColumn + direction)
                                              .Skip(size * (index - 1)).Take(size);

                count = mat.Count();

                return mat.ToList();
            }
        }

        public bool Update(tbl_material model)
        {
            try
            {
                using (var db = new PAVINGDataContext())
                {

                    tbl_material eModel;

                    if (model.material_id == 0)
                    {
                        model.material_create_by = PavingContext.CurrentUserId();
                        model.material_create_date = DateTime.Now;

                        db.tbl_materials.InsertOnSubmit(model);
                    }
                    else
                    {
                        eModel = db.tbl_materials.SingleOrDefault(s => s.material_id.Equals(model.material_id));

                        if (eModel != null)
                        {
                            eModel.material_name = model.material_name;
                            eModel.material_type_id = model.material_type_id;
                            eModel.tbl_mat_prices = model.tbl_mat_prices;

                            eModel.material_update_by = PavingContext.CurrentUserId(); ;
                            eModel.material_update_date = DateTime.Now;
                        }
                    }

                    db.SubmitChanges();

                    return true;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Delete(int[] ids)
        {
            using (var tran = new TransactionScope())
            {
                try
                {
                    using (var db = new PAVINGDataContext())
                    {

                        foreach (var id in ids)
                        {
                            var eModel = db.tbl_materials.SingleOrDefault(s => s.material_id.Equals(id));

                            if (eModel == null) continue;
                            db.tbl_materials.DeleteOnSubmit(eModel);
                            db.SubmitChanges();
                        }

                        tran.Complete();
                        return true;
                    }
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
        }

        public List<tbl_mat_price> GetMaterialPrice(int index,
                                                            int size,
                                                            string sortColumn,
                                                            string sortDirection,
                                                            int matid,
                                                            ref int count)
        {
            using (var db = new PAVINGDataContext())
            {
                var direction = sortDirection.Equals("desc") ? " descending " : "";

                var mat = db.tbl_mat_prices.Where(u => u.material_id == matid)
                                              .OrderBy(sortColumn + direction)
                                              .Skip(size * (index - 1)).Take(size);

                count = mat.Count();

                return mat.ToList();
            }
        }

        public MATERIAL_EXPAND_VIEW GetMatirial(int matId)
        {
            using (var db = new PAVINGDataContext())
            {
                return db.MATERIAL_EXPAND_VIEWs.SingleOrDefault(m => m.material_id == matId);
            }
        }


        #endregion

        #region | Detail |

        public tbl_block GetBlock(int blockId)
        {
            using (var db = new PAVINGDataContext())
            {
                return db.tbl_blocks.SingleOrDefault(m => m.block_id == blockId);
            }
        }

        public List<MATERIAL_EXPAND_VIEW> ColorList(int index,
                                                int size,
                                                string sortColumn,
                                                string sortDirection,
                                                int blockId,
                                                ref int count)
        {
            using (var db = new PAVINGDataContext())
            {
                var direction = sortDirection.Equals("desc") ? " descending " : "";

                var mat = db.MATERIAL_EXPAND_VIEWs.Where(u => u.block_id == blockId)
                                              .OrderBy(sortColumn + direction)
                                              .Skip(size * (index - 1)).Take(size);

                count = mat.Count();

                return mat.ToList();
            }
        }

        #endregion


    }

    //        public List<tbl_block> GetBlocks(ref int count)
    //        {
    //            using (var db = new PAVINGDataContext())
    //            {
    //                count = db.tbl_blocks.Count();
    //                return db.tbl_blocks.ToList();
    //            }
    //        }
    //        public List<tbl_block> GetBlocksByKeywordOrSeries(int index, int size, string sortColumn, string sortDirection, string keyword, int seriesId, ref int count)
    //        {
    //            using (var db = new PAVINGDataContext())
    //            {
    //                var direction = sortDirection.Equals("desc") ? " descending " : "";
    //
    //                if (seriesId != 0 && !string.IsNullOrEmpty(keyword))
    //                {
    //                    //var output = from b in db.tbl_blocks
    //                    //             from s in db.tbl_series
    //                    //             where b.series_id == s.series_id && b.block_name.Contains(keyword)
    //                    //             orderby sortColumn + direction
    //                    //             select b;
    //                    var output = db.tbl_blocks.Where(b => b.series_id == seriesId && b.block_code.Contains(keyword)).OrderBy(sortColumn + direction);
    //                    count = output.Count();
    //                    return output.ToList();
    //
    //                }
    //                if (!string.IsNullOrEmpty(keyword))
    //                {
    //                    /*var output = from b in db.tbl_blocks
    //                               where b.block_name.Contains(keyword)
    //                               select b;*/
    //                    count = db.tbl_blocks.Count(u => u.block_code.ToString().Contains(keyword));
    //                    var output =
    //                        db.tbl_blocks.Where(u => u.block_code.ToString().Contains(keyword)).OrderBy(sortColumn +
    //                                                                                                    direction);
    //                    return output.ToList();
    //                }
    //                if (seriesId == 0)
    //                {
    //                    count = db.tbl_blocks.ToList().Count;
    //                    var output = db.tbl_blocks.OrderBy(sortColumn + direction);
    //                    return output.ToList();
    //                }
    //                else
    //                {
    //                    count = db.tbl_blocks.Count(u => u.series_id == seriesId);
    //                    var output = db.tbl_blocks.Where(u => u.series_id == seriesId).OrderBy(sortColumn + direction);
    //                    return output.ToList();
    //                }
    //            }
    //        }
    //
    //        public List<tbl_block> GetBlocks(int index, int size, string sortColumn, string sortDirection, string keyword, ref int count)
    //        {
    //            using (var db = new PAVINGDataContext())
    //            {
    //                var direction = sortDirection.Equals("desc") ? " descending " : "";
    //                count = db.tbl_blocks.Count(u => u.block_code.ToString().Contains(keyword));
    //
    //                return db.tbl_blocks.Where(u => u.block_code.ToString().Contains(keyword)).OrderBy(sortColumn + direction).ToList();
    //            }
    //        }
    //        public List<tbl_block> GetBlockFromSeries(int seriesId, ref int count)
    //        {
    //            using (var db = new PAVINGDataContext())
    //            {
    //                if (seriesId == 0)
    //                    return db.tbl_blocks.ToList();
    //                count = db.tbl_blocks.Count(u => u.series_id == seriesId);
    //                return db.tbl_blocks.Where(u => u.series_id == seriesId).ToList();
    //            }
    //        }
    //
    //        public string GetSerieNameFromId(int serieId)
    //        {
    //            using(var db = new PAVINGDataContext())
    //            {
    //                var list = db.tbl_series.Where(s => s.series_id == serieId).ToList();
    //                foreach (var item in list)
    //                {
    //                    return item.series_name;
    //                }
    //                return string.Empty;
    //            }
    //        }
}